{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# MinMaxScaler, StandardScaler练习并理解原理",
   "id": "ff1ec96e498e64d0"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-27T14:20:51.177297Z",
     "start_time": "2025-02-27T14:20:51.175368Z"
    }
   },
   "source": [
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
    "# 方差是数据集中每个数据点与平均值（均值）之差的平方的平均值。\n",
    "# 标准差是方差的平方根，单位与原始数据一致。"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 特征处理：也就是把不同的特征拉到同一个量纲：归一化、标准化",
   "id": "e4ef37eceb0274d3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-27T14:21:50.982553Z",
     "start_time": "2025-02-27T14:21:50.979420Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 归一化：\n",
    "# 通过对原始数据进行变换把数据映射到(默认为[0,1])之间\n",
    "def mm():\n",
    "    \"\"\"\n",
    "    归一化处理\n",
    "    :return: NOne\n",
    "    \"\"\"\n",
    "    # 归一化缺点 容易受极值的影响\n",
    "    #feature_range代表特征值范围，一般设置为(0,1),或者(-1,1),默认是(0,1)\n",
    "    mm = MinMaxScaler(feature_range=(0, 1))  #feature_range=(0,1):归一化到0-1之间\n",
    "    # 在训练集上计算最小值和最大值，然后在测试集上使用相同的比例进行缩放\n",
    "    data = mm.fit_transform([[90, 2, 10, 40],\n",
    "                             [60, 4, 15, 45],\n",
    "                             [75, 3, 13, 46]])\n",
    "\n",
    "    print(data)\n",
    "    out = mm.transform([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n",
    "    print(out)\n",
    "    return None\n",
    "    #transform和fit_transform不同是，transform用于测试集，而且不会重新找最小值和最大值\n",
    "\n",
    "\n",
    "mm()"
   ],
   "id": "8033cc0c4742de1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.         0.         0.         0.        ]\n",
      " [0.         1.         1.         0.83333333]\n",
      " [0.5        0.5        0.6        1.        ]]\n",
      "[[-1.96666667  0.         -1.4        -6.        ]\n",
      " [-1.83333333  2.         -0.6        -5.33333333]\n",
      " [-1.7         4.          0.2        -4.66666667]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ",
   "id": "fcc628d8e141635b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-27T14:22:43.134375Z",
     "start_time": "2025-02-27T14:22:43.130828Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准化：通过对原始数据进行变换把数据变换到均值为0,标准差为1范围内.不是标准正态分布，只是均值为0，标准差为1的均匀分布\n",
    "\n",
    "def stand():\n",
    "    \"\"\"\n",
    "    标准化缩放，不是标准正太分布，只均值为0，方差为1的分布\n",
    "    在已有样本足够多的情况下比较稳定，适合现代嘈杂大数据场景。\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    std = StandardScaler()\n",
    "\n",
    "    data = std.fit_transform([[1., -1., 3.],\n",
    "                              [2., 4., 2.],\n",
    "                              [4., 6., -1.]])\n",
    "\n",
    "    print(data)\n",
    "    print('-' * 50)\n",
    "    print(std.mean_)\n",
    "    print('-' * 50)\n",
    "    print(std.var_)\n",
    "    print(std.n_samples_seen_)  # 样本数\n",
    "    return None\n",
    "\n",
    "\n",
    "stand()"
   ],
   "id": "9c395615f0cdb581",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.06904497 -1.35873244  0.98058068]\n",
      " [-0.26726124  0.33968311  0.39223227]\n",
      " [ 1.33630621  1.01904933 -1.37281295]]\n",
      "--------------------------------------------------\n",
      "[2.33333333 3.         1.33333333]\n",
      "--------------------------------------------------\n",
      "[1.55555556 8.66666667 2.88888889]\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
